﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Content;

namespace Deflection
{
    class GameObject
    {
        public Vector2 sizeInPixels;
        public Vector2 SizeInPixels
        {
            get { return sizeInPixels; }
            set { sizeInPixels = value; }
        }
        public Vector2 position ;
        public Vector2 Position
        {
            get { return position; }
            set { position = value; }
        }

        public float Rotation { get; set; }

        public Player player;
        public Vector2 velocity;
        public Vector2 Velocity
        {
            get { return velocity; }
            set { velocity = value; }
        }
        public enum ObjectState
        {
            Alive,
            Dying,
            Dead
        }
        public ObjectState state;
        public static Texture2D Sprite { get; set; }
        protected string spriteName { get; set; }

        protected bool alive { get; set; }

        public bool IsCollidable { get; set; }

        public bool Alive
        {
            get { return alive; }
            set { alive = value; }
        }
        public GameObject(){ }
        
        public GameObject(Vector2 position)
        {
            this.position = position;
            alive = true;
            Sprite = null;
            spriteName = null;
            IsCollidable = true;
            Rotation = 0;
        }

        public virtual void Update(GameTime gameTime)
        {
            if (IsOffscreen())
            {
                // Object is offscreen so 'kill' it
                Die();
            }
        }

        public virtual bool IsOffscreen()
        {
            if (this.position.Y < 0)
            {
                return true;
            }
            if (this.position.X < 0)
            {
                return true;
            }
            if (this.position.Y > GameParameters.SCREEN_SIZE.Y)
            {
                return true;
            }
            if (this.position.X > GameParameters.SCREEN_SIZE.X)
            {
                return true;
            }
            return false;
        }

        public virtual void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            
        }

        public virtual void Die()
        {
            alive = false;
        }

        /// <summary>
        /// Loads all the content needed for this gameobject
        /// </summary>
        /// <param name="content">A content manager</param>
        /// <param name="spriteName">The file name of the sprite</param>
        public static Texture2D LoadContent(ContentManager content, string spriteName)
        {
            return content.Load<Texture2D>(spriteName);        
        }
        public static SoundEffect LoadAudioContent(ContentManager content, string spriteName)
        {
            return content.Load<SoundEffect>(spriteName);
        }

        protected Color Invert(Color aColor)
        {
            Color inverse = new Color(1.0f - aColor.R, 1.0f - aColor.G, 1.0f - aColor.B, 0.0f - aColor.A);
            return inverse;
        }

        public static Vector2 GetRandomTopOfScreenSpawnPosition(Random rand)
        {
            return new Vector2(rand.Next(0,(int)GameParameters.SCREEN_SIZE.X),0);
        }
        public static Vector2 GetRandomBottomOfScreenSpawnPosition(Random rand)
        {
            return new Vector2(rand.Next(0,(int)GameParameters.SCREEN_SIZE.X),GameParameters.SCREEN_SIZE.Y);
        }
        public static Vector2 GetRandomLeftOfScreenSpawnPosition(Random rand)
        {
            return new Vector2(0,rand.Next(0, (int)GameParameters.SCREEN_SIZE.Y));
        }
        public static Vector2 GetRandomRightOfScreenSpawnPosition(Random rand)
        {
            return new Vector2(GameParameters.SCREEN_SIZE.X,rand.Next(0, (int)GameParameters.SCREEN_SIZE.Y));
        }

        /// <summary>
        /// Gets the distance between this GameObject and a vector2 position
        /// </summary>
        /// <param name="vector2">The position to get the distance between</param>
        /// <returns>The distance as a float</returns>
        public float GetDistance(Vector2 vector2)
        {
            return Vector2.Distance(Position, vector2);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="gameObject"></param>
        /// <returns>True if collided</returns>
        public bool CheckForCollision(GameObject gameObject)
        {
            // Need to take  into account the enemy scale
            if (this.GetDistance(gameObject.Position) <= sizeInPixels.X/2 + gameObject.sizeInPixels.X/2)
            {
                //Alive = false;
                return true;
            }
            //Alive = true;
            return false;
        }
    }
}